package com.tencent.wxcloudrun.frontcontroller;

import com.tencent.wxcloudrun.dto.*;
import com.tencent.wxcloudrun.model.Coupons;
import com.tencent.wxcloudrun.service.CouponsService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/3/4 19:04
 * @description: TODO
 */
@RestController
@Api(tags = "红包优惠卷")
@Slf4j
public class CouponsController {


    @Autowired
    private CouponsService couponsService;

    /**
     * 获取可领取优惠券列表
     */
    @Operation(summary = "获取可领取优惠券列表")
    @PostMapping("/wxcloud/coupons")
    CouponsResponse coupons(HttpServletRequest request, @RequestBody CouponsRequest json) {
        String openid = request.getHeader("X-WX-OPENID");
        List<Coupons>  coupons =  couponsService.getCoupons(openid);
        CouponsResponse couponsResponse = new CouponsResponse();
        if (coupons.size()>0){
            couponsResponse.setData(coupons);
            couponsResponse.setCode(0);
            return couponsResponse;
       }else {
            couponsResponse.setCode(2);
            return couponsResponse;
        }
    }
    /**
     * 领取优惠券
     */
    @Operation(summary = "领取优惠券")
    @PostMapping("/wxcloud/fetchCoupons")
    FetchCouponsResponse fetchCoupons(HttpServletRequest request, @RequestBody FetchCouponsRequest json) {
       // String openid = request.getHeader("X-WX-OPENID");
        String openid = "oHLjx5OAcMnZG12Rr_-wXTgD29ys";
        FetchCouponsResponse fetchCouponsResponse = new FetchCouponsResponse();
        String id = json.getId();
        log.info("id:{}",id);
        //检查优惠券是否可领取
        Coupons coupons = couponsService.checkCoupons(id);
        log.info("coupons:{}",coupons);
        if (coupons == null){
           fetchCouponsResponse.setCode(20001);
              fetchCouponsResponse.setMsg("来晚了，优惠券已经被领完了");
            return fetchCouponsResponse;
        }else {
            MyCoupons coupons1 = couponsService.checkCoupons1(openid,id);
            if (coupons1 != null){
                fetchCouponsResponse.setCode(20003);
                fetchCouponsResponse.setMsg("你领过了，别贪心哦");
                return fetchCouponsResponse;
            }else {
                int i = couponsService.fetchCoupons(openid, id);
                if (i == 0){
                    fetchCouponsResponse.setCode(20004);
                    fetchCouponsResponse.setMsg("领取失败");
                    return fetchCouponsResponse;
                }else {
                    fetchCouponsResponse.setCode(0);
                    fetchCouponsResponse.setMsg("领取成功");
                    return fetchCouponsResponse;
                }
            }
        }

    }

    /**
     * 我的优惠券
     */
    @Operation(summary = "我的优惠券")
    @GetMapping("/wxcloud/myCoupons")
    MyCouponsResponse myCoupons(HttpServletRequest request) {
        String openid = request.getHeader("X-WX-OPENID");
        List<MyCoupons> myCoupons = couponsService.myCoupons(openid);
        MyCouponsResponse response = new MyCouponsResponse();
        if (myCoupons.size()>0){
            response.setCode(0);
            response.setData(myCoupons);
            return response;
        }else {
            response.setCode(2);
            return response;
        }
    }

    /**
     * 优惠券统计
     *
     * @param request
     * @return
     */
    @Operation(summary = "微信小程序获取优惠券统计免code")
    @PostMapping(value = "/wxcloud/couponStatistics1")
    CouponStatisticsResponse couponStatistics(HttpServletRequest request) {
        return null;
    }

    @PostMapping(value = "/wxcloud/couponStatistics")
    String test1() {
        return "{\n" +
                "\t\"code\": 200,\n" +
                "\t\"data\": {\n" +
                "\t\t\"canUse\": 0\n" +
                "\t},\n" +
                "\t\"msg\": \"ok\"\n" +
                "}";
    }


}
